package com.xyht.medicine_management_system.modules.payment.wxpay.config;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RSANotificationConfig;
import com.wechat.pay.java.service.payments.app.AppServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.profitsharing.ProfitsharingService;
import com.wechat.pay.java.service.refund.RefundService;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import static com.xyht.medicine_management_system.modules.payment.constant.CommonConstant.*;

@Component
public class WxPayConfig {
    public static JsapiServiceExtension jsapiServiceExtension;
    public static AppServiceExtension appServiceExtension;
    public static RefundService refundService;
    public static ProfitsharingService profitsharingService;
    public static NotificationParser notificationParser;

    /**
     * @description 微信支付初始化配置
     * 平台证书获取步骤：1、下载jar包  https://github.com/wechatpay-apiv3/CertificateDownloader
     * 2、运行jar包 java -jar CertificateDownloader.jar -k ${apiV3key} -m ${mchId} -f ${apiclient_key.pem路径} -s ${证书序列号} -o ${outputFilePath}
     *
     * @create 2023/12/24
     * @author dxn
    */
    @Bean
    public void wxPayInit(){
        String OSName = System.getProperty("os.name");
        String certificatePath = OSName.toLowerCase().startsWith("win") ? WX_PLATE_CERT_PATH_WIN
                : WX_PLATE_CERT_PATH_LINUX;
        String keyPath = OSName.toLowerCase().startsWith("win") ? WX_KEY_PATH_WIN
                : WX_KEY_PATH_LINUX;

        Config rsaConfig =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(WX_SP_MERCHANT_ID)
                        .privateKeyFromPath(keyPath)
                        .merchantSerialNumber(WX_MERCHANT_SERIALNUMBER)
                        .apiV3Key(WX_API_V3_KEY)
                        .build();

        jsapiServiceExtension = new JsapiServiceExtension.Builder().config(rsaConfig).build();
        appServiceExtension = new AppServiceExtension.Builder().config(rsaConfig).build();
        refundService = new RefundService.Builder().config(rsaConfig).build();
        profitsharingService = new ProfitsharingService.Builder().config(rsaConfig).build();

        RSANotificationConfig notificationConfig = new RSANotificationConfig.Builder()
                .certificatesFromPath(certificatePath)
                .apiV3Key(WX_API_V3_KEY)
                .build();
        notificationParser = new NotificationParser(notificationConfig);

    }


}
